home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11561 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: ix.netcom.com!netnews
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Specifying Data Layout
  5. Date: 15 Mar 1996 02:45:02 GMT
  6. Organization: Netcom
  7. Message-ID: <4ialje$3s0@ixnews3.ix.netcom.com>
  8. References: <1996Mar14.173643.8651@nosc.mil>
  9. NNTP-Posting-Host: den-co10-19.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Thu Mar 14  6:45:02 PM PST 1996
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <1996Mar14.173643.8651@nosc.mil>, sampson@cod.nosc.mil says...
  16. >
  17. >     Is there some way of precisely specifying the memory layout of a
  18. >struct?  In other words, can you say, "This struct is to occupy 3 words of
  19. >memory, element foo is to occupy bits 5-12 of word 1, element fern is to
  20. >occupy ..."?  If it can be done with a struct, can it also be done with a
  21. >class (specifying locations for the data members only)?  In this case, can
  22. >you also control the gizmo that is used to distinguish the various values
  23. >of a class hierarchy, both where it is to be allocated and the values to be
  24. >used to identify specific members of the hierarchy?
  25.  
  26.  
  27. First, remember that for simple, non-derived classes with no virtual
  28. methods, the data layout is exactly compatible with "C" for
  29. compatibility reasons.  That being said, the only guarantee that
  30. the language makes is:
  31.  
  32. 1) The elements will appear in memory in the order that they appear
  33.    in the class/struct declaration.
  34.  
  35. 2) The elements will be properly aligned according to the requirements
  36.    of the machine architechure.
  37.  
  38. PC compilers in 16-bit mode typically "pack" structures so that
  39. all elements are byte-aligned (no padding).  On Intel Windows95
  40. and NT, the alignment is typically 2 bytes.  There are compiler
  41. options to control the alignment.  Alignment varies on other
  42. machines.
  43.  
  44. In order to specify the bit locations of elements, I would first
  45. determine (experimentally if needed) the alignment for your target
  46. machine.  Then use the mask-and-shift operations to extract the
  47. bits out of words.  Bitfields don't make any guarantees about
  48. which bits get used, so explicit extraction is better.
  49.  
  50. john lilley
  51.  
  52.